<?php

use \think\Request;
use \think\Db;
use think\Session;
use think\Config;
use think\Controller;
/**
 * @writer:yangjian
 * @DateTime:2018-01-17T11:45:56+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 获取地理位置信息 
 * @param:
 * @return                            
 * [type] 
 * [description]
 */

function getAreaListOld(){
	$Re = Db::name('district')->select();
    return $Re;
}

/**
 * @writer:yangjian
 * @DateTime:2018-01-22T13:48:17+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description:获取一二级地区信息
 * @param:
 * @return
 */
function getAreaList(){
	$arr = array(
			'0' => array('admin_name1' =>'England','admin_name' =>'英格兰'),
			'1' => array('admin_name1' =>'Northern Ireland','admin_name' =>'北爱尔兰'),
			'2' => array('admin_name1' =>'Scotland','admin_name' =>'苏格兰'),
			'3' => array('admin_name1' =>'Wales','admin_name' =>'威尔士')
		);
	$Re  = Db::query("select DISTINCT admin_name1,admin_name2 from v_gb_district where admin_name1  ='England' AND admin_name2  <>''  ORDER BY admin_name2");
	$Re0 = Db::query("select DISTINCT admin_name1,admin_name2,admin_name3 from v_gb_district where admin_name1  ='Northern Ireland'  ORDER BY admin_name2");
	$Re1 = Db::query("select DISTINCT admin_name1,admin_name2,admin_name3 from v_gb_district where admin_name1  ='Scotland'  ORDER BY admin_name2");
	$Re2 = Db::query("select DISTINCT admin_name1,admin_name2,admin_name3 from v_gb_district where admin_name1  ='Wales'  ORDER BY admin_name2");
	$Re3 = Db::query("select DISTINCT admin_name1,admin_name2,admin_name3 from v_gb_district where admin_name1  ='England' AND admin_name2  ='' ORDER BY admin_name3");
	$array = array_merge($Re,$Re3);
	$result['England'] 	= $array;
	$result['Ireland']  = $Re0;
	$result['Scotland'] = $Re1;
	$result['Wales'] 	= $Re2;
	$result['first'] 	= $arr;
	return $result;
}

/**
 * @writer:yangjian
 * @DateTime:2018-01-22T11:59:34+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description:查询三级区域名称 
 * @param:$name 二三级区域名称 
 * @param:
 * @return[type]
 */
function getAreaListByParent($name){
    $Re = Db::table('gb_postal_code')
		->where('admin_name2','=',$name)
		->where('admin_name3','<>','')
		->group('admin_name3')
		->select();
	return $Re;
}
/**
 * @writer:yangjian
 * @DateTime:2018-01-17T17:36:24+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description:
 * @param: id:商户ID               
 * @return                            
 * [type]     [description]
 */
function getSupplierArea($id){
    $Re = Db::table('supplier_send_manage')
	    ->where('supplier_id',$id)
	    ->select();
    return $Re;
}

/**
 * @writer:yangjian
 * @DateTime:2018-01-30T16:51:50+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 根据邮编获取地址列表
 * @param:
 * @param [type] $code 邮编地址
 * @return[type]       [description]
 */
function getAreaInfoByCode($code){
	$code = str_replace(" ",'',$code);
    $Re = Db::table('gb_postal_code')
        ->field('postal_code,place_name,admin_name1,admin_name2,admin_name3')
        ->where('postal_code','like',"{$code}%")
        ->where('admin_name3','<>','')
        ->select();
    foreach ($Re as $k=>$v){
        if (!$v['admin_name1']){
            $Re[$k]['addr'] = $v['place_name'].','.$v['admin_name3'].','.$v['admin_name2'];
        }elseif (!$v['admin_name2']){
            $Re[$k]['addr'] = $v['place_name'].','.$v['admin_name3'].','.$v['admin_name1'];
        }elseif (!$v['admin_name3']){
            $Re[$k]['addr'] = $v['place_name'].','.$v['admin_name2'].','.$v['admin_name1'];
        }else{
            $Re[$k]['addr'] = $v['place_name'].','.$v['admin_name3'].','.$v['admin_name2'].','.$v['admin_name1'];
        }
    }
    return $Re;
}
/**
 * @writer:yangjian
 * @DateTime:2018-01-30T16:51:50+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 获取基础字典数据
 * @param:
 * @param [type]
 * @return[type][description]
 */
function getBase(){
    $Re = Db::table('base_data')
    		->field('id,name,name_english,parent_id,level,leaf,type_menu')
    		->where('flag',1)
	    	->select();
    return $Re;
}
/**
 * @writer:yangjian
 * @DateTime:2018-01-18
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description: 生成随机数
 * @param:
 * @return
 * [type]
 * [description]
 */

function randomNum(){
    $token = MD5(time().rand(100000,999999));
    return $token;
}


function get_getallheaders()  //定义方法
{  
    foreach ($_SERVER as $name => $value)  //循环_SERVER数组
    {  
        if (substr($name, 0, 5) == 'HTTP_')  //前5个字符是HTTP_的进入循环
        {  
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;  
       }  
   }  
   return $headers;  //返回前key前5个字符是HTTP_的数组
   //return $_SERVER; //返回_SERVER数组
}  


/**
 * @writer:yangjian
 * @DateTime:2018-02-23T14:05:37+0800
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description:地址数据重组
 * @param:
 * @param $list  [description]
 * @param $count [description]
 */
function setTree($list,$num){
	$arr = [];
	$search = [];   //标签 
    for ($i=0; $i < $num; $i++) { 
		$name = $list[$i]['admin_name2'];
		if($name==''){ $name = 'other';	}
		$name3= $list[$i]['admin_name3'];
    	$key = array_search($name,$search);
    	$newArr = [];
		if($key===false){
    		$newArr['name']   	= $name;
    		$newArr['nameEn'] 	= $name;
    		$newArr['isParent'] = false;
    		$newArr['children'] 	= [];
    		array_push($arr,$newArr);
			array_push($search,$name);
    	}else{
    		if( $name3 !=='' && $name !==''){
    			$arr[$key]['isParent'] = true;
        		$newArr['name']   	= $name3;
        		$newArr['nameEn'] 	= $name3;
        		$newArr['isParent'] = false;
        		array_push($arr[$key]['children'],$newArr);
    		}
    	}
    }
    return $arr;
}

